﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Restaurant.Client.Framework;
using Restaurant.Common;
using Restaurant.Core;
using Restaurant.Model;
using Restaurant.Client.Framework.Extension;
namespace Restaurant.Client
{
    /// <summary>
    /// w_Login.xaml 的交互逻辑
    /// </summary>
    public partial class w_Login
    {
        const string IdPath = "App_Data/Auth.txt";
        public w_Login()
        {
            InitializeComponent();
        }

        private async void btn_Signin_Click(object sender, RoutedEventArgs e)
        {
            tb_Tip.Text = "";
            btn_Signin.IsEnabled = false;
            var idpath = CommonHelper.MapPath(IdPath);
            string id;
            try
            {
                id = File.ReadAllText(idpath);
            }
            catch (Exception)
            {

                tb_Tip.Text = "参数错误";
                btn_Signin.IsEnabled = true;
                return;
            }

            if (string.IsNullOrEmpty(id))
            {
                tb_Tip.Text = "参数错误";
                btn_Signin.IsEnabled = true;
                return;
            }

            Global.SubCompanyId = new Guid(id);

            var u = txt_Uid.Text.Trim();
            var p = txt_Pwd.Password.Trim();

            if (string.IsNullOrEmpty(u) || string.IsNullOrEmpty(p))
            {
                tb_Tip.Text = "用户名/密码不能为空";
                btn_Signin.IsEnabled = true;
                return;
            }

            var s = new Dictionary<string, object>();
            s.Add("subId", Global.SubCompanyId);
            s.Add("uid", u);
            s.Add("pwd", p);

            OperationResult b = await APIHelper.TryGetAsync<OperationResult>("SignIn/SignIn", s);

            if ("成功" == b.Message)
            {
                var s1 = new Staff();


                var d = b.GetAppData<Staff>();

                DialogResult = true;
                Global.Uid = u;
                Global.Pwd = p;
                Global.Oprator = d.Name;
                Close();
                return;
            }
            else
            {
                tb_Tip.Text = b.Message;
                btn_Signin.IsEnabled = true;
                return;
            }



        }


    }
}
